package com.jackrain.nea.common;

import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.config.PropertiesConf;
import com.jackrain.nea.core.redis.RedisObjectSerializer;
import com.jackrain.nea.web.query.QuerySessionImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import java.net.URI;

/**
 * @author wuwenxue
 * @date 2018/05/18
 **/
@Service
public class PostDubbleEvent {

    @Autowired
    private PropertiesConf pconf;

    @Autowired
    private RestTemplateUtil restTemplateUtil;

    private RedisObjectSerializer serializer = new RedisObjectSerializer();

    public JSONObject executePostDubble(String notic, QuerySessionImpl qsession) throws Exception {

        String fcedas = pconf.getProperty("fc.edas.url", "");
        URI uri = new URI(fcedas + "/fc/api/execommand");

        String[] trigger = notic.split(":");
        MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
        JSONObject paramObject = new JSONObject();
        paramObject.put("address", trigger[0]);
        paramObject.put("group", trigger[2]);
        paramObject.put("version", trigger[1]);
        byte[] eventB = serializer.serialize(qsession.getEvent());
        byte[] userB = serializer.serialize(qsession.getUser());
        paramObject.put("event", new String(eventB, "ISO-8859-1"));
        paramObject.put("user", new String(userB, "ISO-8859-1"));
        map.add("param", paramObject.toJSONString());
        String retStr = restTemplateUtil.doPostForm(uri, map);
        JSONObject retJo = JSONObject.parseObject(retStr);

        return retJo;

    }

}
